package cz.possoft.client.utils;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JApplet;

import cz.possoft.client.gui.panel.GamePanel;

/**
  * Trida plni staticky ucel na dotaz pro aktualni instance jednotlivych panelu
  * 
  * @author mara
  */
public class PanelHolderUtils {

	// aktualni instance zobrazeneho herniho panelu
	private static GamePanel currentGamePanel;
	
	// aktualni instance pusteneho appletu (aplikace)
	private static JApplet applet;
	
	// pole vsech hracich panelu
	private static Map<String, GamePanel> gamePanels = new HashMap<String, GamePanel>();
	
	// metoda inicializuje vsechny hraci panely a vlozi je do pole gamePanels
	public static void initAllGamePanels() {
		for (String gameKey : GameInvocationUtils.getGames().keySet()) {
			GamePanel gamePanel = GameInvocationUtils.getInstanceOfGamePanel(gameKey);
			gamePanel.setGameClassPrefix(gameKey);
			gamePanel.init();			
			gamePanels.put(gameKey, gamePanel);
			gamePanel.setVisible(false);
		}
	}
	
	// vraci vsechny gamePanely v kolekci. Pouzito v PossoftNetChess appletu pri inicializaci k vlozeni techto panelu
	// do appletu.
	public static Collection<GamePanel> getAllGamePanels() {
		return gamePanels.values();
	}
	
	// jako currentGamePanel nastavi panel z listu gamePanels podle predlozeneho keye
	// Navic vraci tento gamePanel
	public static GamePanel setCurrentGamePanelFromPrefix(String gameKey) {
		        GamePanel gamePanel = gamePanels.get(gameKey); 
				setCurrentGamePanel(gamePanel);											
		        return gamePanel;
	}
	
	// nastavuje aktualni instanci zobrazeneho herniho panelu
	public static void setCurrentGamePanel(GamePanel currGP) {
		if (currentGamePanel != null) {			
			currentGamePanel.setVisible(false);			
		}
		currentGamePanel = currGP;
		currentGamePanel.setVisible(true);
	}
	
	// aktualni instance zobrazeneho herniho panelu
	public static GamePanel getCurrentGamePanel() {
		return currentGamePanel;
	}	

	public static JApplet getApplet() {
		return applet;
	}

	public static void setApplet(JApplet applet) {
		PanelHolderUtils.applet = applet;
	}		
	
}
